<template>
  <div class="update-gender">
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      @cancel="$emit('close')"
      @confirm="onConfirm"
      @change="onPickerChange"
      :default-index="value"
    />

    <!--  @confirm="onConfirm"
      @cancel="onCancel"
      @change="onChange"
       -->
  </div>
</template>

<script>
import { updateUserProfile } from "@/api/user.js";

export default {
  name: "UpdateGender",
  data() {
    return {
      columns: ["男", "女"],
      localGender: this.value,
    };
  },
  props: {
    value: {
      type: Number,
      required: true,
    },
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: "提交中...",
        forbidClick: true,
        duration: 0,
      });
      try {
        const localGender = this.localGender;
        await updateUserProfile({
          gender: localGender
        });
        console.log(localGender);
        // 更新视图
        this.$emit("input", localGender);
        // 关闭弹层
        this.$emit("close");
        // 提示成功
        this.$toast.success("更新成功!");
      } catch (err) {
        this.$toast.fail("更新失败!");
      }
    },

    onPickerChange(picker, value, index) {
      // picker: 组件对象
      // value 值
      // index: 索引
      // console.log(picker,value,inedx);
      this.localGender = index;
    },
  },
};
</script>

<style></style>
